<template>
  <div>
    <el-dialog
      class="add_dialog"
      width="600px"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :rules="editItem?rules1:rules"
        :model="form"
        label-width="100px"
      >
        <el-form-item
          label="账号:"
          prop="mobile"
        >
          <template v-if="!editItem">
            <el-input
              maxlength="11"
              v-model="form.mobile"
              placeholder="请输入手机号"
              @input="handleMobileChange"
            ></el-input>
          </template>
          <span v-else>{{form.mobile}}</span>
        </el-form-item>
        <el-form-item
          label="姓名/备注:"
          prop="username"
        >
          <el-input
            maxlength="10"
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码:"
          prop="pwd"
        >
          <el-input
            type="password"
            :placeholder="editItem?'(选填)留空则不修改':'请输入密码'"
            v-model="form.pwd"
            autocomplete="new-password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item
          label="重复密码:"
          prop="confirmPwd"
        >
          <el-input
            type="password"
            :placeholder="editItem?'(选填)留空则不修改':'请再次输入密码'"
            v-model="form.confirmPwd"
            autocomplete="new-password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item
          label="角色:"
          prop="role_id"
        >
          <el-select
            v-model="form.role_id"
            placeholder="请选择角色"
          >
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          size="small"
          @click="dialogVisible = false"
        >取 消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="submit"
        >保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { editUser, addUser } from "@/api/system";
import { checkMobile } from '@/utils/validate';
export default {
  props: ["editItem", "roleList"],
  data() {
    let validateMobile = (rule, value, callback) => {
      if (!checkMobile(value)) {
        callback(new Error('手机号格式错误'));
      } else {
        callback();
      }
    }
    var validateConfirm = (rule, value, callback) => {
      if (value !== this.form.pwd) {
        callback(new Error('两次输入密码不同！'));
      } else {
        callback();
      }
    }
    var validatePwd = (rule, value, callback) => {
      let reg = /^[^\u4e00-\u9fa5]{0,}$/;
      if (value && value.length < 6) {
        callback(new Error('密码必须大于等于6个字符'))
      } else if (!reg.test(value)) {
        callback(new Error('密码不能包含汉字！'));
      } else {
        callback();
      }
    }
    return {
      dialogVisible: false,
      form: {
        username: "",
        pwd: "",
        confirmPwd: "",
        role_id: null,
        mobile: ""
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        pwd: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validatePwd, trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, message: '重复密码不能为空', trigger: 'blur' },
          { validator: validateConfirm, trigger: 'blur' }
        ],
        role_id: [{ required: true, message: '请选择角色', trigger: 'change' }],
      },
      rules1: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        pwd: [{ validator: validatePwd, trigger: 'blur' }],
        confirmPwd: [
          { validator: validateConfirm, trigger: 'blur' }
        ],
        role_id: [{ required: true, message: '请选择角色', trigger: 'change' }],
      },
    }
  },
  computed: {
    dialogTitle() {
      return this.editItem ? "编辑账号" : "添加账号";
    }
  },
  methods: {
    handleMobileChange() {
      this.form.mobile = this.form.mobile.replace(/[^\d]/g, "");
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.editItem) {
            editUser({
              id: this.form.id,
              username: this.form.username,
              pwd: this.form.pwd ? this.$md5(this.form.pwd) : "",
              role_id: this.form.role_id
            }).then(res => {
              this.dialogVisible = false;
              this.$emit("getList");
            })
          } else {
            addUser({
              mobile: this.form.mobile,
              username: this.form.username,
              pwd: this.$md5(this.form.pwd),
              role_id: this.form.role_id
            }).then(res => {
              this.dialogVisible = false;
              this.$emit("getList");
            })
          }
        } else {
          this.$message({
            message: "请检查表单错误",
            type: "error"
          });
          return false;
        }
      });
    },
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs['form'].resetFields();
          if (this.editItem) {
            // 编辑
            this.form = {
              id: this.editItem.id,
              username: this.editItem.username,
              mobile: this.editItem.mobile,
              role_id: this.editItem.role_id
            }
          }
        });
      } else {
        this.form = {
          mobile: "",
          username: "",
          pwd: "",
          confirmPwd: "",
          role_id: null
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
